package com.water.devtool.ui.define;

import org.eclipse.swt.widgets.Composite;

import com.water.devtool.define.ModularBean;
import com.water.devtool.ui.UiUtil;

import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class ModularComposite extends Composite implements TabComposite {

	private ModularBean modular;
	private Text text_code;
	private Text text_desc;
	private Text text_datasource;
	private Text text_seqname;

	/**
	 * Create the composite.
	 * 
	 * @param parent
	 * @param style
	 */
	public ModularComposite(Composite parent, int style) {
		super(parent, style);

		Label label = new Label(this, SWT.NONE);
		label.setText("名称：");
		label.setAlignment(SWT.RIGHT);
		label.setBounds(30, 44, 98, 20);

		text_code = new Text(this, SWT.BORDER);
		text_code.setBounds(133, 41, 349, 26);

		Label label_1 = new Label(this, SWT.NONE);
		label_1.setText("描述：");
		label_1.setAlignment(SWT.RIGHT);
		label_1.setBounds(56, 252, 76, 20);

		text_desc = new Text(this, SWT.BORDER | SWT.WRAP | SWT.V_SCROLL | SWT.MULTI);
		text_desc.setBounds(137, 252, 352, 117);

		Button button = new Button(this, SWT.NONE);
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				modular.setCode(text_code.getText());
				modular.setDesc(text_desc.getText());
				modular.setDefaultDataSource(text_datasource.getText());
				modular.setDefaultSeqName(text_seqname.getText());

			}
		});
		button.setText("确定");
		button.setBounds(364, 403, 98, 30);

		Button button_1 = new Button(this, SWT.NONE);
		button_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				closeTab();
			}
		});
		button_1.setText("取消");
		button_1.setBounds(632, 401, 98, 30);

		Label label_2 = new Label(this, SWT.NONE);
		label_2.setText("数据源：");
		label_2.setAlignment(SWT.RIGHT);
		label_2.setBounds(30, 104, 98, 20);

		text_datasource = new Text(this, SWT.BORDER);
		text_datasource.setBounds(133, 101, 349, 26);

		Label label_3 = new Label(this, SWT.NONE);
		label_3.setText("序列：");
		label_3.setAlignment(SWT.RIGHT);
		label_3.setBounds(500, 107, 98, 20);

		text_seqname = new Text(this, SWT.BORDER);
		text_seqname.setBounds(613, 104, 349, 26);

	}

	@Override
	protected void checkSubclass() {
	}

	public void setModular(ModularBean modular) {
		if (this.modular != null) {
			return;
		}
		this.modular = modular;
		UiUtil.setText(text_code, modular.getCode());
		UiUtil.setText(text_desc, modular.getDesc());
		UiUtil.setText(text_datasource, modular.getDefaultDataSource());
		UiUtil.setText(text_seqname, modular.getDefaultSeqName());

	}

	public void closeTab() {
		TabFolder tabFolder = (TabFolder) getParent();
		TabItem[] items = tabFolder.getSelection();
		for (TabItem tabItem : items) {
			tabItem.dispose();
		}
	}

}
